package weaver.ihg.action;

import cn.hutool.http.HttpUtil;
import cn.hutool.json.JSONUtil;
import weaver.conn.RecordSet;
import weaver.formmode.customjavacode.AbstractModeExpandJavaCodeNew;
import weaver.general.BaseBean;
import weaver.general.Util;
import weaver.hrm.User;
import weaver.soa.workflow.request.Property;
import weaver.soa.workflow.request.RequestInfo;
import weaver.file.Prop;

import java.util.*;

public class WebTaskSignleServletFormAction extends AbstractModeExpandJavaCodeNew {
    BaseBean logger = new BaseBean();

    private final static String ActionName = " WebTaskSignleServletFormAction V1 ";

    @Override
    public Map<String, String> doModeExpand(Map<String, Object> param) {
        String cjrFiledName = "hdcjr";
        String rwmcFiledName = "rwmc";
        String bgrwjdFiledName = "bgrwjd";
        Map<String, String> result = new HashMap<String, String>();
        logger.writeLog(ActionName + " start ..." + "===hdcjr===>" + cjrFiledName);
        try {
            User user = (User) param.get("user");
            logger.writeLog(ActionName + " current operate user: " + user.getLoginid());
            int billid = -1;//数据id
            int modeid = -1;//模块id
            RequestInfo requestInfo = (RequestInfo) param.get("RequestInfo");

            if (requestInfo != null) {
                billid = Util.getIntValue(requestInfo.getRequestid());
                modeid = Util.getIntValue(requestInfo.getWorkflowid());

                logger.writeLog(ActionName + " billid:" + billid + ", modeid:" + modeid);

                if (billid > 0 && modeid > 0) {
                    BaseBean baseBean = new BaseBean();
                    baseBean.writeLog("1111===>" + JSONUtil.toJsonStr(requestInfo));
                    baseBean.writeLog("param===>" + JSONUtil.toJsonStr(param));
                    String requestHost = Prop.getPropValue("cofigHostApi", "apiUrl");
                    baseBean.writeLog("requestHost===>" + requestHost);

                    List<String> psList = new ArrayList<>();
                    psList.add(cjrFiledName);
                    psList.add(rwmcFiledName);
                    psList.add(bgrwjdFiledName);
                    //修改参数字段大小写111
                    Map<String, Object> masterFiledStr = showFormProperty(psList, requestInfo, baseBean);
                    //获取表单存放的对应任务流程的requestId
                    String requestId = String.valueOf(masterFiledStr.get("rwmc"));
                    //通过requestId获取当前节点Id和workflowId
                    RecordSet rs = new RecordSet();
                    String getRequestSql = "select * from workflow_requestbase where requestid=?";
                    rs.executeQuery(getRequestSql, requestId);
                    rs.next();
                    String currentNodeid = rs.getString("lastnodeid");
                    String workflowid = rs.getString("workflowid");
                    //拼接请求url并发起请求
                    String apiUrl = requestHost + "changeOpertorMethod" + "?requestid=" + requestId + "&nodeid=" + currentNodeid + "&workflowid=" + workflowid
                            + "&field5=" + masterFiledStr.get(cjrFiledName) + "&operate=save&forwardflag=3&newNodeId=" + masterFiledStr.get(bgrwjdFiledName) + "&operateUser=" + user.getUID();
                    baseBean.writeLog("apiUrl===>" + apiUrl);
                    String apiResponseBody = HttpUtil.createGet(apiUrl).execute().body();
                    baseBean.writeLog("apiResponseBody===>" + apiResponseBody);
                }
            }
        } catch (Exception e) {
            result.put("errmsg", e.getMessage());
            result.put("flag", "false");
            logger.errorLog(ActionName + " result:" + JSONUtil.toJsonStr(result), e);
        }
        return result;
    }

    /**
     * 获取主表数据
     */
    private Map<String, Object> showFormProperty(List<String> needPrams, RequestInfo requestInfo, BaseBean baseBean) {
        baseBean.writeLog("needParams==>" + JSONUtil.toJsonStr(needPrams));
        Map<String, Object> resultMap = new HashMap<>();
        baseBean.writeLog("获取主表数据 ...");
        // 获取表单主字段值
        Property[] properties = requestInfo.getMainTableInfo().getProperty();
        baseBean.writeLog("properties==>" + Arrays.toString(properties));
        for (Property property : properties) {
            // 主字段名称
            String name = property.getName();
            // 主字段对应的值
            String value = com.weaver.general.Util.null2String(property.getValue());
            baseBean.writeLog("name: " + name + "===>value: " + value);
            for (String needPram : needPrams) {
                if (name.equalsIgnoreCase(needPram)) {
                    resultMap.put(name, value);
                }
            }


        }
        return resultMap;
    }

}
